
//
//  RootViewController.m
//  NewProject
//
//  Created by  on 13-11-29.
//  Copyright (c) 2013年 Steven. All rights reserved.
//

#import "QRViewController.h"

@interface QRViewController ()


@end

@implementation QRViewController

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    self.view.backgroundColor = [UIColor clearColor];
    
    [self setSelfView];
    
    upOrdown = NO;
    num =0;
    _line = [[UIImageView alloc] initWithFrame:CGRectMake(50, 130, WINDOWWIDTH - 100, 2)];
    _line.image = [UIImage imageNamed:@"line.png"];
    [_line setBackgroundColor:[UIColor whiteColor]];
    [_line setAlpha:0.5];
    [self.view addSubview:_line];
    
    timer = [NSTimer scheduledTimerWithTimeInterval:.02 target:self selector:@selector(animation1) userInfo:nil repeats:YES];
    
}

- (void)setSelfView
{
    UIView *viewHead = [[UIView alloc] initWithFrame:CGRectMake(0, 0, WINDOWWIDTH, 62)];
    [viewHead setBackgroundColor:[UIColor whiteColor]];
    [self.view addSubview:viewHead];
    
    UIButton *btnBack = [[UIButton alloc] initWithFrame:CGRectMake(0, 20, 60, 42)];
    [btnBack setImage:[UIImage imageNamed:@"topbar_back.png"] forState:UIControlStateNormal];
    [btnBack addTarget:self action:@selector(backAction) forControlEvents:UIControlEventTouchUpInside];
    [viewHead addSubview:btnBack];
    
    UILabel *labHead = [[UILabel alloc] initWithFrame:CGRectMake(60, 20, WINDOWWIDTH - 120, 42)];
    [labHead setBackgroundColor:[UIColor clearColor]];
    [labHead setTextColor:[UIColor blackColor]];
    [labHead setTextAlignment:NSTextAlignmentCenter];
    [labHead setText:@"二维码"];
    [viewHead addSubview:labHead];
    
    UIImageView * imageView = [[UIImageView alloc]initWithFrame:CGRectMake(10, 100, WINDOWWIDTH - 20, WINDOWWIDTH - 20)];
    imageView.image = [UIImage imageNamed:@"pick_bg"];
    [self.view addSubview:imageView];
    
    UIView *view1 = [[UIView alloc] initWithFrame:CGRectMake(0 , 62, WINDOWWIDTH, 98)];
    [view1 setBackgroundColor:[UIColor blackColor]];
    [view1 setAlpha:0.4];
    [self.view addSubview:view1];
    
    UIView *view2 = [[UIView alloc] initWithFrame:CGRectMake(0 , 160, 50, WINDOWWIDTH - 100)];
    [view2 setBackgroundColor:[UIColor blackColor]];
    [view2 setAlpha:0.4];
    [self.view addSubview:view2];

    UIView *view3 = [[UIView alloc] initWithFrame:CGRectMake(WINDOWWIDTH - 50 , 160, 50, WINDOWWIDTH - 100)];
    [view3 setBackgroundColor:[UIColor blackColor]];
    [view3 setAlpha:0.4];
    [self.view addSubview:view3];
    
    UIView *view4 = [[UIView alloc] initWithFrame:CGRectMake(0 , WINDOWWIDTH + 60, WINDOWWIDTH, WINDOWHiGHT - WINDOWWIDTH - 60)];
    [view4 setBackgroundColor:[UIColor blackColor]];
    [view4 setAlpha:0.4];
    [self.view addSubview:view4];
    
    UILabel * labIntroudction= [[UILabel alloc] initWithFrame:CGRectMake(15, WINDOWWIDTH + 60, WINDOWWIDTH - 30, 50)];
    labIntroudction.backgroundColor = [UIColor clearColor];
    labIntroudction.numberOfLines=2;
    labIntroudction.textAlignment = NSTextAlignmentCenter;
    labIntroudction.textColor=[UIColor whiteColor];
    labIntroudction.text=NSLocalizedString(@"Please put the 2D code picture in the box", @"");
    [self.view addSubview:labIntroudction];

    
}

-(void)animation1
{
    if (upOrdown == NO) {
        num ++;
        _line.frame = CGRectMake(50, 160+2*num, WINDOWWIDTH -100, 2);
        if (2*num == WINDOWWIDTH - 100) {
            upOrdown = YES;
        }
    }
    else {
        num --;
        _line.frame = CGRectMake(50, 160+2*num, WINDOWWIDTH -100, 2);
        if (num == 0) {
            upOrdown = NO;
        }
    }

}
-(void)backAction
{
    
    [self dismissViewControllerAnimated:YES completion:^{
        [timer invalidate];
    }];
}

-(void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    [self setupCamera];
}
/**调用系统二维码扫描*/
- (void)setupCamera
{
    // Device
    _device = [AVCaptureDevice defaultDeviceWithMediaType:AVMediaTypeVideo];
    
    // Input
    _input = [AVCaptureDeviceInput deviceInputWithDevice:self.device error:nil];
    
    // Output
    _output = [[AVCaptureMetadataOutput alloc]init];
    [_output setMetadataObjectsDelegate:self queue:dispatch_get_main_queue()];
    
    // Session
    _session = [[AVCaptureSession alloc]init];
    [_session setSessionPreset:AVCaptureSessionPresetHigh];
    if ([_session canAddInput:self.input])
    {
        [_session addInput:self.input];
    }
    
    if ([_session canAddOutput:self.output])
    {
        [_session addOutput:self.output];
    }
    
    // 条码类型 AVMetadataObjectTypeQRCode
    _output.metadataObjectTypes =@[AVMetadataObjectTypeQRCode];
    
    // Preview
    _preview =[AVCaptureVideoPreviewLayer layerWithSession:self.session];
    _preview.videoGravity = AVLayerVideoGravityResizeAspectFill;
    _preview.frame =CGRectMake(0,62,WINDOWWIDTH,WINDOWHiGHT);
    [self.view.layer insertSublayer:self.preview atIndex:0];
    

    
    // Start
    [_session startRunning];
}
#pragma mark AVCaptureMetadataOutputObjectsDelegate
- (void)captureOutput:(AVCaptureOutput *)captureOutput didOutputMetadataObjects:(NSArray *)metadataObjects fromConnection:(AVCaptureConnection *)connection
{
   
    NSString *stringValue;
    
    if ([metadataObjects count] >0)
    {
        AVMetadataMachineReadableCodeObject * metadataObject = [metadataObjects objectAtIndex:0];
        stringValue = metadataObject.stringValue;
    }
    
    [_session stopRunning];
    
    if (self.mdelegate && [self.mdelegate respondsToSelector:@selector(onScanQRViewBack:)]) {
        [self.mdelegate onScanQRViewBack:stringValue];
    }
    
   [self dismissViewControllerAnimated:YES completion:^
    {
        [timer invalidate];
        NSLog(@"%@",stringValue);
    }];
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end
